package com.linewow.xhyy.nihaonew.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liangfeizc.flowlayout.FlowLayout;
import com.linewow.xhyy.hrxb.DemoHelper;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.TagDetailActivity;
import com.linewow.xhyy.nihaonew.activity.TagDetailCustomActivity;
import com.linewow.xhyy.nihaonew.activity.TranslateActivity;
import com.linewow.xhyy.nihaonew.activity.UserMineActivity;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.db.NiHaoHelper;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.image.ImagesInfo;
import com.linewow.xhyy.nihaonew.entity.tag.TagListInfo;
import com.linewow.xhyy.nihaonew.entity.topic.TopicDetailInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;
import com.linewow.xhyy.nihaonew.view.ColorfulTag;
import com.linewow.xhyy.nihaonew.view.VoiceButton2Red;
import com.squareup.picasso.Picasso;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/7.
 */
public class TopicDetailAdapter extends RecyclerView.Adapter {
    private LayoutInflater layoutInflater;
    private Context context;
    private String TAG="TopicDetailAdapter";
    private String key="msg";

    private List<TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>> topicDetailInfos;
    private ArrayList<CustomTagsInfo> list;

    public enum ITEM_TYPE {
        ITEM1,
        ITEM2
    }

    public TopicDetailAdapter(Context context, List<TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>> topicDetailInfos) {
        this.context=context;
        this.layoutInflater = LayoutInflater.from(this.context);
        this.topicDetailInfos = topicDetailInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
            View view = layoutInflater.inflate(R.layout.item_topic_detail_head, null);
            return new HeadViewHolder(view);
        } else {
            View view = layoutInflater.inflate(R.layout.item_topic_detail_follow, null);
            return new FollowViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>topicDetailInfo=topicDetailInfos.get(position);
        if(holder instanceof HeadViewHolder){
            bindHeadView((HeadViewHolder) holder,topicDetailInfo);
        }else if(holder instanceof FollowViewHolder){
            ((FollowViewHolder) holder).item2TopicDetailImgsRecycler.setAdapter(new ImagesVerticalAdapter(context,topicDetailInfo.images));
            bindFootView((FollowViewHolder) holder,topicDetailInfo);
        }
    }

    private void bindFootView(FollowViewHolder holder, TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo> topicDetailInfo) {
        Picasso.with(context).load((HttpModel.HOMEURL+topicDetailInfo.avatar).replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_avatar).into(holder.item2TopicDetailAvatar);
        holder.item2TopicDetailNickname.setText(topicDetailInfo.uinfo.nickname);
        holder.item2TopicDetailTimeTv.setText(topicDetailInfo.timestr);

        if(topicDetailInfo.audiotime.equals("0")){
            holder.item2wBtn.setVisibility(View.GONE);
        }else{
            holder.item2wBtn.setVisibility(View.VISIBLE);
            holder.item2wBtn.setUrlPath(topicDetailInfo.audiotime,topicDetailInfo.audiopath);
        }


        if(!topicDetailInfo.prid.equals("0")){
            holder.item2TopicDetailNicknameReply.setVisibility(View.VISIBLE);
            holder.item2TopicDetailNicknameReply.setText(context.getString(R.string.title_reply)+topicDetailInfo.tnickname);
        }else{
            holder.item2TopicDetailNicknameReply.setVisibility(View.GONE);
        }

        holder.item2TopicDetailContentTranslateRela.setVisibility(View.VISIBLE);
        if(topicDetailInfo.subject.equals("null")){
            if(topicDetailInfo.contents.equals("")){
                holder.item2TopicDetailContentTranslateRela.setVisibility(View.GONE);
            }else{
                holder.item2TopicDetailContent.setText(topicDetailInfo.contents);
            }
        }else{
            holder.item2TopicDetailContent.setText(topicDetailInfo.subject);
        }



        holder.item2TopicDetailContent.setText(topicDetailInfo.contents);
        holder.item2TopicDetailImgsRecycler.setAdapter(new ImagesVerticalAdapter(context,topicDetailInfo.images));

        holder.item2TopicDetailAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG,"打印测试头像点击");
            }
        });
    }

    private void bindHeadView(HeadViewHolder viewHolder,TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>topicDetailInfo) {
        Picasso.with(context).load(HttpModel.HOMEURL+topicDetailInfo.avatar).fit().error(R.mipmap.err_avatar).into(viewHolder.item1TopicDetailAvatar);
        viewHolder.item1TopicDetailNickname.setText(topicDetailInfo.uinfo.nickname);
        viewHolder.item1TopicDetailTimeTv.setText(topicDetailInfo.timestr);

        if(topicDetailInfo.audiotime.equals("0")){
            viewHolder.item1TopicDetailAudioBtn.setVisibility(View.GONE);
        }else{
            viewHolder.item1TopicDetailAudioBtn.setVisibility(View.VISIBLE);
            viewHolder.item1TopicDetailAudioBtn.setUrlPath(topicDetailInfo.audiotime,topicDetailInfo.audiopath);
        }


        viewHolder.item1TopicDetailContent.setVisibility(View.VISIBLE);
        if(topicDetailInfo.subject.equals("null")){
            if(topicDetailInfo.contents.equals("")){
                viewHolder.item1TopicDetailContent.setVisibility(View.GONE);
            }else{
                viewHolder.item1TopicDetailContent.setText(topicDetailInfo.contents);
            }
        }else{
            viewHolder.item1TopicDetailContent.setText(topicDetailInfo.subject);
        }


        if(topicDetailInfo.links.equals("null")){
            viewHolder.item1TopicDetailNet.setVisibility(View.GONE);
        }else{
            viewHolder.item1TopicDetailNet.setVisibility(View.VISIBLE);
            viewHolder.item1TopicDetailNet.setText(topicDetailInfo.links);
        }
        if(topicDetailInfo.location.equals("null")){
            viewHolder.item1TopicDetailAddressLine.setVisibility(View.GONE);
        }else{
            viewHolder.item1TopicDetailAddressLine.setVisibility(View.VISIBLE);
            viewHolder.item1TopicDetailAddressTv.setText(topicDetailInfo.location);
        }
        if(topicDetailInfo.zan.equals("0")){
            viewHolder.widgetTopicMenuLikeImg.setImageResource(R.mipmap.icon_like_gray);
        }else{
            viewHolder.widgetTopicMenuLikeImg.setImageResource(R.mipmap.icon_like_red);
        }
        viewHolder.widgetTopicMenuTopicTv.setText(topicDetailInfo.replynum);
        viewHolder.widgetTopicMenuLikeTv.setText(topicDetailInfo.upnum);
        viewHolder.item1TopicDetailImgsRecycler.setAdapter(new ImagesVerticalAdapter(context,topicDetailInfo.images));
        setCustomTags(viewHolder.item1TopicDetailFlowlayout,topicDetailInfo.tags,topicDetailInfo.customtags);
        if(list.size()==0){
            viewHolder.itemTopicDetailIconTagRela.setVisibility(View.GONE);
        }else{
            viewHolder.itemTopicDetailIconTagRela.setVisibility(View.VISIBLE);
        }
    }

    private void setCustomTags(FlowLayout item1TopicDetailFlowlayout,List<CustomTagsInfo> tags, List<CustomTagsInfo> customtags) {
        if(list==null){

            list=new ArrayList<CustomTagsInfo>();
            list.addAll(tags);
            list.addAll(customtags);
            for(CustomTagsInfo temp:list){
                Log.e(TAG,"所有的tag"+temp.zhcn);
                ColorfulTag colorfulTag = new ColorfulTag(context);
//                colorfulTag.setHei(dp2px(15));
                colorfulTag.setCustomTagsInfo(temp);
                item1TopicDetailFlowlayout.addView(colorfulTag);
                colorfulTag.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CustomTagsInfo info = (CustomTagsInfo) view.getTag();
                        Intent intent=new Intent();
                        TagListInfo info1=new TagListInfo();
                        info1.setZhcn(info.zhcn);
                        info1.setEn(info.en);
                        info1.setTagid(info.tagid);
                        Log.e("TopicDetailAdapter","是否自定义"+info.iscustom);
                        if(info.iscustom){
                            intent.setClass(context, TagDetailCustomActivity.class);
                        }else{
                            intent.setClass(context,TagDetailActivity.class);
                        }
                        intent.putExtra(key,info1);
                        context.startActivity(intent);

                    }
                });
            }
        }

    }

    public int dp2px(int dpValue) {
        return (int) (dpValue * context.getResources().getDisplayMetrics().density);
    }

    @Override
    public int getItemCount() {
        return topicDetailInfos.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (position==0)?ITEM_TYPE.ITEM1.ordinal():ITEM_TYPE.ITEM2.ordinal();
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item1_topic_detail_avatar)
        ImageView item1TopicDetailAvatar;
        @Bind(R.id.item1_topic_detail_nickname)
        TextView item1TopicDetailNickname;
        @Bind(R.id.item1_topic_detail_address_tv)
        TextView item1TopicDetailAddressTv;
        @Bind(R.id.item1_topic_detail_address_line)
        LinearLayout item1TopicDetailAddressLine;
        @Bind(R.id.item1_topic_detail_translate_tv)
        TextView item1TopicDetailTranslateTv;
        @Bind(R.id.item1_topic_detail_time_tv)
        TextView item1TopicDetailTimeTv;
        @Bind(R.id.item1_topic_detail_content)
        TextView item1TopicDetailContent;
        @Bind(R.id.item1_topic_detail_net)
        TextView item1TopicDetailNet;
        @Bind(R.id.item1_topic_detail_imgs_recycler)
        RecyclerView item1TopicDetailImgsRecycler;
        @Bind(R.id.item1_topic_detail_flowlayout)
        FlowLayout item1TopicDetailFlowlayout;
        @Bind(R.id.widget_topic_menu_topic_img)
        ImageView widgetTopicMenuTopicImg;
        @Bind(R.id.widget_topic_menu_topic_tv)
        TextView widgetTopicMenuTopicTv;
        @Bind(R.id.widget_topic_menu_like_img)
        ImageView widgetTopicMenuLikeImg;
        @Bind(R.id.widget_topic_menu_like_tv)
        TextView widgetTopicMenuLikeTv;
        @Bind(R.id.widget_topic_menu_conversation_img)
        ImageView widgetTopicMenuConversationImg;
        @Bind(R.id.item1_topic_detail_audio_btn)
        VoiceButton2Red item1TopicDetailAudioBtn;
        @Bind(R.id.item1_topic_detail_icon_tag)
        ImageView item1TopicDetailIconTag;
        @Bind(R.id.item1_topic_detail_icon_tag_rela)
        RelativeLayout itemTopicDetailIconTagRela;

        HeadViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            item1TopicDetailImgsRecycler.setItemAnimator(new DefaultItemAnimator());
            LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            item1TopicDetailImgsRecycler.setLayoutManager(manager);
            widgetTopicMenuTopicImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new MessageEvent(),"topic");
                }
            });

            widgetTopicMenuLikeImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



                    final TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>info=topicDetailInfos.get(0);
                    if(NiHaoApp.flag){
                        if(info.zan.equals("0")){
                            Log.e(TAG,"点赞");
                            NiHaoOkgo.zanInfo(info)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<NiHaoGeneral<String>>() {
                                        @Override
                                        public void call(NiHaoGeneral<String> stringNiHaoGeneral) {

                                            if(stringNiHaoGeneral.status){
                                                info.setZan("1");
                                                int upnum=Integer.parseInt(info.upnum);
                                                info.setUpnum(Integer.toString(++upnum));
                                                widgetTopicMenuLikeImg.setImageResource(R.mipmap.icon_like_red);
                                                widgetTopicMenuLikeTv.setText(info.upnum);
                                                NiHaoHelper.getInstance().requestZan(info.uid,info.rid);

                                            }
                                        }
                                    });

                        }else{

                            NiHaoOkgo.zanInverseInfo(info)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<NiHaoGeneral<String>>() {
                                        @Override
                                        public void call(NiHaoGeneral<String> stringNiHaoGeneral) {

                                            if(stringNiHaoGeneral.status){
                                                info.setZan("0");
                                                int upnum=Integer.parseInt(info.upnum);
                                                info.setUpnum(Integer.toString(--upnum));
                                                widgetTopicMenuLikeImg.setImageResource(R.mipmap.icon_like_gray);
                                                widgetTopicMenuLikeTv.setText(info.upnum);

                                            }
                                        }
                                    });

                        }
                    }else {
                        DemoHelper.getInstance().onConnectionConflict();
                    }
                }
            });

            widgetTopicMenuConversationImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new MessageEvent(),"conversation");

                }
            });
            item1TopicDetailTranslateTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    translateActivity();
                }
            });


            item1TopicDetailAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e(TAG,"点击了头像");
                    TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>info=topicDetailInfos.get(0);
                    Uinfo uinfo=info.uinfo;
                    startUserMain(uinfo);
                }
            });
        }
    }

    private void startUserMain(Uinfo uinfo) {
        Intent intent=new Intent(context, UserMineActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelable("info",uinfo);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    class FollowViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item2_topic_detail_avatar)
        ImageView item2TopicDetailAvatar;
        @Bind(R.id.item2_topic_detail_nickname)
        TextView item2TopicDetailNickname;
        @Bind(R.id.item2_topic_detail_time_tv)
        TextView item2TopicDetailTimeTv;
        @Bind(R.id.item2_topic_detail_content)
        TextView item2TopicDetailContent;
        @Bind(R.id.item2_topic_detail_imgs_recycler)
        RecyclerView item2TopicDetailImgsRecycler;
        @Bind(R.id.item2_topic_detail_audio_btn)
        VoiceButton2Red item2wBtn;
        @Bind(R.id.item2_topic_detail_reply)
        ImageView item2TopicDetailReply;
        @Bind(R.id.item2_topic_detail_content_translate_rela)
        RelativeLayout item2TopicDetailContentTranslateRela;
        @Bind(R.id.item2_topic_detail_content_translate)
        ImageView item2TopicDetailContentTranslate;
        @Bind(R.id.item2_topic_detail_nickname_reply)
        TextView item2TopicDetailNicknameReply;

        FollowViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            item2TopicDetailImgsRecycler.setLayoutManager(manager);
            item2TopicDetailImgsRecycler.setItemAnimator(new DefaultItemAnimator());

            item2TopicDetailReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG,"打印了- 回复--》"+FollowViewHolder.this.getAdapterPosition());
                    MessageEvent messageEvent=new MessageEvent("");
                    messageEvent.setIndex(FollowViewHolder.this.getAdapterPosition());
                    EventBus.getDefault().post(messageEvent,"Topic");
                }
            });

            item2TopicDetailContentTranslate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    int position=FollowViewHolder.this.getAdapterPosition();
                    TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>topicDetailInfo=topicDetailInfos.get(position);
                    Intent intent=new Intent(context, TranslateActivity.class);
                    if(topicDetailInfo.subject.equals("null")){
                        intent.putExtra("info",topicDetailInfo.contents);
                    }else{
                        intent.putExtra("info",topicDetailInfo.subject);
                    }
                    context.startActivity(intent);

                }
            });


        }
    }

    public void translateActivity(){
        TopicDetailInfo<ImagesInfo, Uinfo, CustomTagsInfo>topicDetailInfo=topicDetailInfos.get(0);
        Intent intent=new Intent(context, TranslateActivity.class);
        if(topicDetailInfo.subject.equals("null")){
            intent.putExtra("info",topicDetailInfo.contents);
        }else{
            intent.putExtra("info",topicDetailInfo.subject);
        }
        context.startActivity(intent);

    }
}
